<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Task;
use App\Models\BuyerTask;
use App\Models\User;
use Auth,DB;

class TaskController extends Controller
{
    
	/**
	 * 任务大厅
	 */
    public function getList(Request $request)
    {
    	$rs['status']    = false;

        $where['status'] = 1;
        $query      = Task::where($where);
        if ($request->keyword) {
            $query = $query->where('name','like','%'.$request->keyword.'%');
        }

        if ($request->platform_id) {
            $query = $query->where('platform_id',$request->platform_id);
        }
        //dd($request->all());
        if ($request->timesort) {
          
            $query = $query->orderBy('created_at',$request->timesort);
        }

        if ($request->commissionsort) {
           
            $query = $query->orderBy('commission',$request->commissionsort);
        }

        $list = $query->paginate(10);
                            
        if($list) {
            $rs['status'] = true;
            $rs['data']   = $list;
            return response()->json($rs);
        }                    
    	return response()->json($rs);
    }

    /**
     * 待完成任务 进行中的任务
     */
    public function getWait()
    {
    	$rs['status'] = 0;
        $where['buyer_id'] = Auth::user()->buyer->id;
        $where['status']   = '0';
    	$rs['data'] = BuyerTask::where('buyer_id',$buyer_id)
    							->with('task')
    							->paginate(10)
    							->toJson();
    	return response()->json($rs);    	
    }

    /**
     * 已取消的任务
     */
    public function getCancle()
    {
        $rs['status'] = 0;
        $where['buyer_id'] = Auth::user()->buyer->id;
        $where['status']   = '-1';
        $rs['data'] = BuyerTask::where('buyer_id',$buyer_id)
                                ->with('task')
                                ->paginate(10)
                                ->toJson();
        return response()->json($rs);       
    }

    /**
     * 任务详情
     */
    public function detail($task_id)
    {
        $rs['status'] = false;
        $data = Task::find($task_id);
        if($data->status == 1) {
            $rs['data']   = $data;
            $rs['status'] = true;
            return response()->json($rs);
        } else {
           $rs['msg'] = '任务以停止';
           return response()->json($rs); 
        }
        return response()->json($rs);
    }

    /**
     * 领取任务
     */
    public function getTask($task_id)
    {
        $rs['status'] = false;
       
        $data['buyer_id'] = Auth::user()->buyer->id;
        $user = User::find(Auth::user()->id);
        if($user->status==0){
            $rs['msg'] = '您的账号正在审核中，通过审核才能接单哦';
            return response()->json($rs);
        }

        //先查一查有没有领取过这个任务
        $map['buyer_id'] = $data['buyer_id'];
        $map['task_id']  = (int)$task_id;
        //dd($map);
        $hadget = BuyerTask::where($map)->count();
       
        if ($hadget) {
            $rs['msg'] = '您已经参与了次任务，不能重复参与哦';
            return response()->json($rs);
        }

        


        $task = Task::find($task_id);
        
        if (!$task) {
            $rs['msg'] = '任务不存在';
            return response()->json($rs);    
        }
        // 验证任务是否还在进行
        if($task->status!==1){
            $rs['msg'] = '当前任务已结束';
            return response()->json($rs);
        }

        // 验证当前任务是否超时
        //dd(strtotime($task->endtime));
        if(time()-strtotime($task->endtime)>0){
            $rs['msg'] = '当前任务已经截止';
            return response()->json($rs);
        }

        // 验证可用任务数量 
        if($task->valid_num==0) {
            $rs['msg'] = '当前任务已抢完';
           
            return response()->json($rs);
        }

        // 15天内如果领取过这个老板的任务那么就不能再次领取，但是管理员发布的除外
        $w['seller_id'] = $task->seller_id;
        $w['buyer_id']  = $data['buyer_id'];
        $buyed = BuyerTask::where($w)->orderBy('created_at','desc')->first();
        if($buyed){
            $now      = time();
            $buytime  = strtotime($buyed->created_at);
            // if($now-$buytime<60*60*24*15 && $task->is_admin==0) {
            //     $rs['msg'] = '十五天内不能接同一卖家的任务';
            //     return response()->json($rs);
            // }
            


        }


        //一旦返现可用任务数量大于0 那么-1
        $task->valid_num = $task->valid_num-1;
        if( $task->valid_num == 0) {
            $task->status = 100;    
        }
        
       
        $task->save();

        $data['task_id']   = $task_id;
        $data['seller_id'] = $task->seller_id;
        $data['price']     = $task->price;
        $data['commission']= $task->commission;
        $flag = BuyerTask::create($data);

        if($flag){
            $rs['status'] = true;
            return response()->json($rs);
        }
    }
}
